package ynu.edu.dish.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.dish.dto.DishDTO;
import ynu.edu.dish.service.DishService;
import ynu.edu.dish.vo.DishVO;

import java.util.List;

/**
 * 菜品控制器
 */
@RestController
@RequestMapping("/dishes")
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    /**
     * 创建菜品
     */
    @PostMapping
    public Result<DishVO> createDish(@Validated(DishDTO.Create.class) @RequestBody DishDTO dishDTO, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 设置商家ID
        dishDTO.setMerchantId(merchantId);
        
        // 创建菜品
        DishVO dishVO = dishService.createDish(dishDTO);
        return Result.success(dishVO);
    }

    /**
     * 更新菜品
     */
    @PutMapping("/{id}")
    public Result<DishVO> updateDish(@PathVariable Long id, @RequestBody @Valid DishDTO dishDTO, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 设置ID和商家ID
        dishDTO.setId(id);
        dishDTO.setMerchantId(merchantId);
        
        // 更新菜品
        DishVO dishVO = dishService.updateDish(dishDTO);
        return Result.success(dishVO);
    }

    /**
     * 获取菜品信息
     */
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 删除菜品
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDish(@PathVariable Long id, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 删除菜品
        boolean result = dishService.deleteDish(id);
        return Result.success(result);
    }

    /**
     * 根据商家ID查询菜品列表（需要认证）
     */
    @GetMapping
    public Result<List<DishVO>> getDishesByMerchantId(HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 查询菜品列表
        List<DishVO> dishVOs = dishService.getDishesByMerchantId(merchantId);
        return Result.success(dishVOs);
    }

    /**
     * 根据商家ID查询菜品列表（公开接口）
     */
    @GetMapping("/merchant/{merchantId}")
    public Result<List<DishVO>> getDishesByMerchantIdPublic(@PathVariable Long merchantId) {
        // 查询菜品列表
        List<DishVO> dishVOs = dishService.getDishesByMerchantIdAndStatus(merchantId, 1);
        return Result.success(dishVOs);
    }

    /**
     * 根据分类ID查询菜品列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<DishVO>> getDishesByCategoryId(@PathVariable Long categoryId) {
        // 查询菜品列表
        List<DishVO> dishVOs = dishService.getDishesByCategoryIdAndStatus(categoryId, 1);
        return Result.success(dishVOs);
    }

    /**
     * 根据商家ID分页查询菜品列表
     */
    @GetMapping("/list/merchant")
    public Result<Page<DishVO>> getDishesByMerchantIdPage(
            @RequestParam Long merchantId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 查询菜品分页列表
        Page<DishVO> dishPage = dishService.getDishesByMerchantId(merchantId, page, size);
        return Result.success(dishPage);
    }

    /**
     * 根据分类ID分页查询菜品列表
     */
    @GetMapping("/list/category")
    public Result<Page<DishVO>> getDishesByCategoryIdPage(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 查询菜品分页列表
        Page<DishVO> dishPage = dishService.getDishesByCategoryId(categoryId, page, size);
        return Result.success(dishPage);
    }

    /**
     * 根据菜品名称模糊查询
     */
    @GetMapping("/search")
    public Result<Page<DishVO>> searchDishesByName(
            @RequestParam String name,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 查询菜品分页列表
        Page<DishVO> dishPage = dishService.searchDishesByName(name, page, size);
        return Result.success(dishPage);
    }

    /**
     * 根据商家ID和菜品名称模糊查询
     */
    @GetMapping("/search/merchant")
    public Result<Page<DishVO>> searchDishesByMerchantIdAndName(
            @RequestParam Long merchantId,
            @RequestParam String name,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 查询菜品分页列表
        Page<DishVO> dishPage = dishService.searchDishesByMerchantIdAndName(merchantId, name, page, size);
        return Result.success(dishPage);
    }

    /**
     * 更新菜品状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateDishStatus(
            @PathVariable Long id,
            @RequestParam Integer status,
            HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 更新菜品状态
        boolean result = dishService.updateDishStatus(id, status);
        return Result.success(result);
    }

    /**
     * 更新菜品推荐状态
     */
    @PutMapping("/{id}/recommended")
    public Result<Boolean> updateDishRecommendedStatus(
            @PathVariable Long id,
            @RequestParam Integer isRecommended,
            HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 更新菜品推荐状态
        boolean result = dishService.updateDishRecommendedStatus(id, isRecommended);
        return Result.success(result);
    }

    /**
     * 更新菜品特价状态
     */
    @PutMapping("/{id}/special")
    public Result<Boolean> updateDishSpecialStatus(
            @PathVariable Long id,
            @RequestParam Integer isSpecial,
            @RequestParam(required = false) Double specialPrice,
            HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 更新菜品特价状态
        boolean result = dishService.updateDishSpecialStatus(id, isSpecial, specialPrice);
        return Result.success(result);
    }

    /**
     * 根据商家ID查询推荐菜品
     */
    @GetMapping("/recommended/{merchantId}")
    public Result<List<DishVO>> getRecommendedDishesByMerchantId(@PathVariable Long merchantId) {
        // 查询推荐菜品列表
        List<DishVO> dishVOs = dishService.getRecommendedDishesByMerchantId(merchantId);
        return Result.success(dishVOs);
    }

    /**
     * 根据商家ID查询特价菜品
     */
    @GetMapping("/special/{merchantId}")
    public Result<List<DishVO>> getSpecialDishesByMerchantId(@PathVariable Long merchantId) {
        // 查询特价菜品列表
        List<DishVO> dishVOs = dishService.getSpecialDishesByMerchantId(merchantId);
        return Result.success(dishVOs);
    }
} 